package com.baitiaojun.common.utils.asserts;

import com.baitiaojun.common.enums.impl.ServiceExpEnums;
import com.baitiaojun.common.exception.ServiceException;
import com.baitiaojun.common.utils.collection.CollectionUtils;
import com.baitiaojun.common.utils.string.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 * 业务异常断言工具
 */
public class AssertUtils {

    /**
     * 是否是true,若不是,则抛出异常
     * @param condition
     * @param businessExpEnums
     */
    public static void isTrue(boolean condition, ServiceExpEnums businessExpEnums) {
        if (!condition) {
            throw new ServiceException(businessExpEnums);
        }
    }

    /**
     * 是否是false,是若不是则抛出异常
     * @param condition
     * @param businessExpEnums
     */
    public static void isFalse(boolean condition, ServiceExpEnums businessExpEnums) {
        if (condition) {
            throw new ServiceException(businessExpEnums);
        }
    }

    /**
     * 如果是空或者是空字符串，则抛异常
     * @param str
     * @param businessExpEnums
     */
    public static void isEmpty(String str, ServiceExpEnums businessExpEnums) {
        if (StringUtils.isEmpty(str)) {
            throw new ServiceException(businessExpEnums);
        }
    }

    /**
     * 如果集合为空或者长度小于1，则抛异常
     * @param collection
     * @param businessExpEnums
     */
    public static void isEmpty(Collection<?> collection, ServiceExpEnums businessExpEnums) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new ServiceException(businessExpEnums);
        }
    }

    /**
     * 如果集合为空或者长度小于1，则抛异常
     * @param map
     * @param businessExpEnums
     */
    public static void isEmpty(Map<?, ?> map, ServiceExpEnums businessExpEnums) {
        if (CollectionUtils.isEmpty(map)) {
            throw new ServiceException(businessExpEnums);
        }
    }

    /**
     * 如果size长度为0，则抛异常
     * @param size
     * @param businessExpEnums
     */
    public static void isEmpty(int size, ServiceExpEnums businessExpEnums) {
        if (size == 0) {
            throw new ServiceException(businessExpEnums);
        }
    }

    /**
     * 如果对象时空，则抛异常
     * @param object
     * @param businessExpEnums
     */
    public static void isNull(Object object, ServiceExpEnums businessExpEnums) {
        if (object == null) {
            throw new ServiceException(businessExpEnums);
        }
    }



}
